package no.uials.distsys;

import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author Martinsen
 */
public class User implements java.io.Serializable, Comparable<User> {
    
    //<editor-fold defaultstate="collapsed" desc="public enum Role{USER, ADMIN};">
    public enum Role {
        USER,
        ADMIN;
        
        public String toString() {
            String text = super.toString();
            return text.charAt(0) + text.substring(1).toLowerCase();
        }
        
        public String toLowerCase() {
            return super.toString().toLowerCase();
        }
    }
    //</editor-fold>
    private String name;
    private String pw;
    private ArrayList<Role> roles = new ArrayList<>();
    
    /**
     * Makes a new instance of User.
     * 
     * @param name the name of the user.
     * @param pw the chosen (and verified) password.
     * @param roles an array of roles the User is allowed.
     */
    public User(String name, String pw, Role... roles) {
        this.name = name;
        this.pw = pw;
        this.roles.addAll(Arrays.asList(roles));
    }
    
    /**
     * Makes a new instance of User.
     * 
     * @param name the name of the User.
     * @param pwthe chosen (and verified) password.
     * @param role the role the User is allowed.
     */
    public User(String name, String pw, Role role) {
        this.name = name;
        this.pw = pw;
        roles.add(role);
    }
    
    /**
     * Returns the User's name.
     * 
     * @return the name.
     */
    public String getName() {
        return name;
    }
    
    /**
     * Adds a set of roles to the User.
     * 
     * @param roles the set of roles.
     */
    public void addRole(Role... roles) {
        this.roles.addAll(Arrays.asList(roles));
    }
    
    /**
     * Removes the given role from the User.
     * 
     * @param role 
     */
    public void removeRole(Role role) {
        roles.remove(role);
    }
    
    /**
     * Checks if the User has the given role.
     * Returns true if the User has the role.
     * 
     * @param role the role in question.
     * @return if the User is in the goven role.
     */
    public boolean isUserInRole(Role role) {
        return roles.contains(role);
    }
    
    /**
     * Validates the input password.
     * Retuns true if the given password is the same as the User's password.
     * 
     * @param passphrase the passphrase to check.
     * @return true if correct passphrase is input.
     */
    public boolean validatePw(String passphrase) {
        return passphrase.compareTo(pw) == 0;
    }
    
    /**
     * Changes the User's password.
     * The old password must be the same as the new password, or the password won't be changed.
     * 
     * @param oldPw the old password for validation.
     * @param newPw the new password to use.
     * @return true if the password was successfully changed.
     */
    public boolean changePwd(String oldPw, String newPw) {
        if (validatePw(oldPw)) {
            pw = newPw;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * Compares this User to another User.
     * 
     * @param user the User to compare this User to.
     * @return the integer representation of the difference between the Users.
     */
    public int compareTo(User user) {
        return name.compareTo(user.name);
    }
    
    /**
     * Retruns a String representation of the User.
     * 
     * @return the String representation of the User.
     */
    public String toString() {
        String retVal = name;
        for (Role role : roles) {
            retVal += ", " + role.toLowerCase();
        }
        return retVal;
    }
}